package com.tsu.mall.order.Controller;


import com.tsu.mall.common.entity.Attachment;
import com.tsu.mall.common.entity.Wares;
import com.tsu.mall.common.service.IWaresService;
import com.tsu.mall.common.service.IAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/wares")
public class WaresController {
    @Autowired
    private IWaresService iWaresService;
    @Autowired
    private IAttachmentService iAttachmentService;
    //根据不同的二级分类的id对应的不同的商品的信息，渲染不同的商品详情页
    @GetMapping("/singleProductPage")
    public String singleProduct(Integer id, Model model) {
       Wares waresInfo=iWaresService.doSelectById(id);
       model.addAttribute("waresInfo",waresInfo);
       //查询商品图图片 轮播图展示
        List<Attachment> pics=iAttachmentService.selectWaresPicsByWaresId(id);
        model.addAttribute("pics",pics);
       return "single-product";
    }


    //查询所有二级分类id下的所有商品
    @PostMapping("/waresList")
    @ResponseBody
    public List<Wares> waresList(Integer id){
        return iWaresService.selectBySecondId(id);
    }

    //获取每个一级分类对应的所有二级分类中最新的哪一个分类中最便宜的商品
    @PostMapping("/cheapWares")
    @ResponseBody
    public List<Wares> cheapWares(){
       return  iWaresService.selectCheapWares();
    }

}

